//
//  TestSounds.cpp
//  NanoIOS
//
//  Created by Jeppe Nielsen on 9/30/13.
//  Copyright (c) 2013 Jeppe Nielsen. All rights reserved.
//

#include "Engine.hpp"
#include "GameWorld.hpp"
#include "SoundSystem.hpp"
#include "RenderSystem.hpp"
#include "FirstPersonMoverSystem.hpp"

using namespace Nano;

class TestSounds : public GameState {

    GameWorld world;
    GameObject* soundEmitter;
    RenderSystem* renderer;
    GameObject* camera;
    void Initialized() {
        
        world.CreateSystem<SoundSystem>();
        renderer = world.CreateSystem<RenderSystem>();
        world.CreateSystem<FirstPersonMoverSystem>()->Input = &Input;
        
        GameObject* sound = world.CreateObject();
        sound->AddComponent<Sound>()->LoadFromWav("drumloop.wav");
        
        soundEmitter = world.CreateObject();
        soundEmitter->AddComponent<Sound>(sound);
        soundEmitter->AddComponent<SoundEmitter>();
        soundEmitter->AddComponent<Transform>();
        soundEmitter->AddComponent<Mesh>()->AddCube(0, 1);
        soundEmitter->AddComponent<Material>();
        
        
        camera = world.CreateObject();
        camera->AddComponent<Transform>();
        camera->AddComponent<Camera>()->Viewport = Viewport();
        camera->AddComponent<FirstPersonMover>();
        camera->GetComponent<Transform>()->Position = Vector3(0,0,4);
        camera->AddComponent<SoundListener>();
        
        
        Input.TouchDown += event_handler(this, &TestSounds::TouchDown);
    }
    
    void TouchDown(TouchEvent e) {
        if (e.Index == 0) return;
        //soundEmitter->GetComponent<SoundEmitter>()->MaxDistance += 1.0f;
        GameObject* clone = soundEmitter->Clone();
        clone->GetComponent<Transform>()->Position = camera->GetComponent<Transform>()->Position;
        // soundEmitter->GetComponent<SoundEmitter>()->Playing = !soundEmitter->GetComponent<SoundEmitter>()->Playing();
    }
    
    
    void Update(float dt) {
        world.Update(dt);
    }
    
    void Render() {
        glClearColor( 0.0f, 0.2f, 0.0f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_CULL_FACE);
        renderer->Render();
    }
    
};


int main() {
    Engine e;
    e.Start<TestSounds>();
    return 0;
}